package com.lantosec.hmrp.modules.cms.controller;


import org.springframework.web.bind.annotation.RequestMapping;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.modules.cms.entity.CmsArticle;
import com.lantosec.hmrp.modules.cms.service.ICmsArticleService;
import com.lantosec.hmrp.modules.cms.vo.CmsArticleVO;

import java.util.Arrays;
import java.util.List;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.RestController;
import com.lantosec.hmrp.core.mvc.BaseController;

/**
 *
 * CMS文章列表 控制器
 *
 * @author AutoCode
 * @since 2020-07-06
 */
@Api(value = "CMS文章列表控制器", tags = "CMS文章列表控制器")
@RestController
@RequestMapping("/cms/article")
@Log4j2
public class CmsArticleController extends BaseController {
    @Autowired
    private ICmsArticleService service;


    @GetMapping("getCmsArticle")
    @ApiOperation(value = "查询栏目下的文章列表")
    public Result<List<CmsArticleVO>> getCmsArticle(@ApiParam(value = "栏目ID") @RequestParam(name="categoryId",required = false) String categoryId,
                                                    @ApiParam(value = "关键字") @RequestParam(name="content",required = false) String content){
        List<CmsArticleVO> cmsArticleVOS = service.getCmsArticle(categoryId,content);
        return Result.ok(cmsArticleVOS);
    }

    @PostMapping("saveCmsArticle")
    @ApiOperation(value = "新增CMS文章数据")
    public Result<Void> saveCmsArticle(@ApiParam(value = "CMS文章列表VO对象", required = true) @RequestBody @Validated CmsArticleVO vo){
        service.saveCmsArticle(vo);
        return Result.ok();
    }

    @PutMapping("update")
    @ApiOperation(value = "修改CMS文章数据")
    public Result<Void> update(@ApiParam(value = "CMS文章列表VO对象", required = true) @RequestBody CmsArticleVO vo){
        CmsArticle entity = vo.convertToEntity();
        service.updateById(entity);
        return Result.ok();
    }

    @DeleteMapping(value = "delete/{ids}")
    @ApiOperation("逻辑删除")
    public Result<Void> delete(@ApiParam(value = "ID列表,用逗号分隔", required = true) @PathVariable(name="ids") String ids){
        if(StringUtils.isNotEmpty(ids)){
            service.removeByIds(Arrays.asList(ids.split(",")));
        }
        return Result.ok();
    }
}
